<!DOCTYPE HTML>
<html>
<head>
<title><%=title%></title>
<meta name='referrer' content='always'/>
<link rel='shortcut icon' href='<%=icon%>'/>
<meta http-equiv='x-ua-compatible' content='ie=edge,chrome=1'/>
<meta http-equiv='content-type' content='text/html; charset=utf-8'/>
<meta name='viewport' content='width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no'/>

<link rel='stylesheet' type='text/css' href='/app/workspace/css/base.css'/>
<link rel='stylesheet' type='text/css' href='/res/lib/highlight/css/tomorrow.css'/>

<script>
if (typeof(require) == 'function') delete window.module;
</script>

<script src='/res/lib/utils.js.gzip'></script>
<script src='/res/lib/laydate/laydate.js.gzip'></script>
<script src='/res/lib/highlight/highlight.js.gzip'></script>

<style>
.MenuItem{
	text-align: center;
}
.MenuIcon{
	width: 36px;
	height: 24px;
	display: inline-block;
	background-size: 60% 88%;
	background-position: center;
	background-repeat: no-repeat;
}
.MenuTitle{
	font-size: 0.7rem;
}
.SubMenuItem{
	margin: 8px;
	padding: 4px;
	text-align: center;
}
.SubMenuIcon{
	width: 60px;
	height: 40px;
	display: inline-block;
	background-size: 56% 80%;
	background-position: center;
	background-repeat: no-repeat;
}
.SubMenuTitle{
	font-size: 0.8rem;
}
#SinglePageDiv{
	padding-bottom: 32px;
}
#SinglePageMenuDiv{
	left: 0px;
	width: 100%;
	bottom: 0px;
	padding: 2px;
	position: fixed;
	background: #FFFFFF;
	border-top: 1px solid #DDEEFF;
}
</style>

<script>
var curmenu = null;
var saveneeded = null;
var cursubmenu = null;
var curmenuitem = null;
var submenuicon = null;

function updateMenu(){
	curmenu = null;
	cursubmenu = null;
	curmenuitem = null;
	submenuicon = null;

	$('#MenuList').html('');

	getHttpResult('/getusermenu', null, function(data){
		if (data.code < 0){
			showToast('加载数据失败');
		}
		else{
			var menu = null;
			var folder = getSession('folder');

			$.each(data.list, function(idx, item){
				var tmp = addMenu(item.folder, item.icon);
				if (folder == item.folder) menu = tmp;
			});

			selectMenu(menu || $('.MenuItem').first());

			$('.MenuItem').click(function(){
				selectMenu($(this));
			});
		}
	});
}
function getSideWidth(){
	return $('#MainDiv').position().left;
}
function updateTitle(){
	$('#TitleDiv').html(getHttpResult('/titlepage'));
}
function updateFooter(){
	$('#BottomDiv').load('/sharenote?flag=S&title=FOOTER');
}
function setSaveNeeded(needed){
	saveneeded = needed;
}
function autoModifyContentSize(id){
}
function addMenu(folder, icon){
	$('#MenuList').append("<td class='MenuItem' title='" + folder + "'><span class='MenuIcon' style='background-image:url(" + icon + ")'></span><div></div><span class='MenuTitle'>" + folder + "</span></td>");
	removeSession('usersubmenu:' + folder);
	return $('.MenuItem').last();
}
function addSubMenu(idx, text, url, icon){
	if (idx % 4 == 0) $('#SubMenuTable').append('<tr></tr>');
	if (text){
		$('#SubMenuTable').last('tr').append("<td class='SubMenuItem' title='" + text + "' link='" + url + "'><span class='SubMenuIcon' style='background-image:url(" + icon + ")'></span><div></div><span class='SubMenuTitle'>" + text + "</span></td>");
	}
	else{
		$('#SubMenuTable').last('tr').append("<td class='SubMenuItem'><span class='SubMenuIcon'></span><div></div><span class='SubMenuTitle'></span></td>");
	}
}
function selectMenu(menu, updated){
	var folder = menu.attr('title');

	if (updated || curmenu == null || curmenu != folder){
		function update(data){
			if (data.code == XG_TIMEOUT){
				sessionTimeout();
			}
			else if(data.code < 0){
				showToast("加载菜单数据失败");
			}
			else{
				cursubmenu = null;
				curmenu = folder;

				setSession('folder', folder);

				if (data.code == 0){
					sessionTimeout();
				}
				else if (data.code == 1){
					getHttpResult(data.list[0].url, {}, function(data){
						$('#SinglePageDiv').html("<div style='text-align:center'><span style='text-align:left'>" + data + "</span></div>");
					}, true);
				}
				else{
					if (data.code > 0){
						var num = 0;

						$.each(data.list, function(idx, item){
							addSubMenu(num++, item.title, item.url, item.icon);
						});
						
						while (num % 4) addSubMenu(num++);
					}

					$('.SubMenuItem').click(function(){
						selectSubMenu($(this));
					});
				}
			}

			return data.code;
		};

		$('#SinglePageDiv').html("<table width='100%' id='SubMenuTable'></table>");

		if (updated) removeSession('usersubmenu:' + folder);

		try{
			update(JSON.parse(getSession('usersubmenu:' + folder)));
		}
		catch(e){
			getHttpResult('/getusermenu', {folder: folder}, function(data){
				if (update(data) > 0) setSession('usersubmenu:' + folder, JSON.stringify(data));
			});
		}
	}

	curmenuitem = menu;
	$('.MenuItem').css('background', 'none');
	menu.css('background', '#789');
}
function selectSubMenu(submenu, updated){
	var path = null;
	var icon = null;
	var title = null;

	if (submenu){
		path = submenu.attr('link');
		title = submenu.attr('title');
		icon = getBackgroundImage(submenu.children().get(0));
	}

	if (updated || submenu || cursubmenu == null){
		cursubmenu = path;
		submenuicon = icon;
		clearSingletonInterval();

		if (strlen(path) > 0){
			window.location.href = '/singlepage?align=center&path=' + encodeURIComponent(path) + '&icon=' + encodeURIComponent(icon) + '&title=' + encodeURIComponent(title);
		}
	}
}

window.onload = function(){
	var code = '<%=code%>';
	var mail = '<%=session%>';

	if (mail.length > 0 && code.length > 0){
		getHttpResult('/checklogin', {mail: mail, code: code}, function(data){
			window.location.href = '/';
		});
	}

	updateMenu();
}
</script>
</head>

<body>
	<div id='SinglePageDiv'></div>
	<div id='SinglePageMenuDiv'>
		<table width='100%'><tr id='MenuList'></tr></table>
	</div>
</body>
</html>